package com.example.demo.mapper;

import com.example.demo.mapper.entity.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Lucy
 * @create 2024-03-14 0:31
 */
@Mapper
@Repository
public interface AdMapper {
    @Select("SELECT * FROM ad LIMIT 3")
    List<Ad> getAdList();
    @Select("SELECT * FROM ad WHERE ad_id = #{adId}")
    Ad selectAdById(int adId);
    @Select("SELECT * FROM ad_setting ORDER BY setting_id DESC LIMIT 1")
    AdSetting selectSetting();
    @Insert("INSERT INTO ad_click (ad_id, student_id, click_cost, IP_address, device_info) " +
            "VALUES (#{adId}, #{studentId}, #{clickCost}, #{IPAddress}, #{deviceInfo}) " )
    @Options(useGeneratedKeys = true, keyProperty = "clickId", keyColumn = "click_id")
    void insertAdClick(AdClick adClick);
    @Insert("INSERT INTO ad_transaction (advertiser_id, amount, transaction_type) " +
            "VALUES (#{advertiserId}, #{amount}, #{transactionType}) " )
    @Options(useGeneratedKeys = true, keyProperty = "transactionId", keyColumn = "transaction_id")
    void insertAdTransaction(AdTransaction transaction);
    @Select("SELECT * FROM advertiser WHERE advertiser_id = #{advertiserId}")
    Advertiser selectAdvertiserById(int advertiserId);
    @Update("UPDATE advertiser SET balance = balance - #{clickCost}, version = version + 1 WHERE advertiser_id = #{advertiserId} AND version = #{currentVersion}")
    int deductClickCost(@Param("advertiserId")int advertiserId, @Param("clickCost")double clickCost, @Param("currentVersion")int currentVersion);
    @Update("UPDATE ad SET ad_status = #{status} WHERE advertiser_id = #{advertiserId}")
    void updateAdStatus(@Param("advertiserId")int advertiserId, @Param("status")int status);
    @Select("SELECT * FROM ad_click")
    List<AdClick> selectAllAdClick();
    @Select("SELECT * FROM student WHERE id = #{studentId}")
    Student selectStudentById(int studentId);
    @Select("SELECT COUNT(*) FROM student WHERE gender = #{gender}")
    int selectStudentCountByGender(int gender);
    @Select("SELECT COUNT(*) FROM student WHERE native_place = #{nativePlace}")
    int selectStudentCountByNativePlace(int nativePlace);
    @Select("SELECT COUNT(*) FROM student WHERE college_id = #{collegeId}")
    int selectStudentCountByCollege(int collegeId);
    @Select("SELECT * FROM ad_key")
    List<AdKey> selectAllAdKey();
    @Select("SELECT * FROM ad_key WHERE ad_id = #{adId}")
    AdKey selectAdKeyById(int adId);
    @Select("SELECT * FROM ad_student WHERE student_id = #{studentId} ORDER BY priority DESC")
    List<AdStudent> selectAdStudent(int studentId);
}
